﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;

namespace Stee.CAP8.Communication.HttpClient
{
    /// <summary>
    /// Extensions methods to set Timeout for <see cref="HttpRequestMessage"/> />.
    /// </summary>
    public static class HttpRequestExtensions
    {

        private static string TimeoutPropertyKey = "RequestTimeout";

        public static void SetTimeout(this HttpRequestMessage request, TimeSpan? timeout)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            request.Properties[TimeoutPropertyKey] = timeout;
        }

        public static TimeSpan? GetTimeout(this HttpRequestMessage request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (request.Properties.TryGetValue(TimeoutPropertyKey, out var value)
                && value is TimeSpan timeout)
            {
                return timeout;
            }

            return null;
        }

    }
}
